package top.continew.admin.req.wecom;

import cn.hutool.core.bean.BeanUtil;
import cn.shengchao.examstar.rpc.wecom.req.RuleConditionGroupSaveDto;
import cn.shengchao.examstar.rpc.wecom.req.RuleConditionSaveDto;
import cn.shengchao.examstar.rpc.wecom.req.RuleDataSaveDto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * @program: examstar-manger-api
 * @description: 规则请求保存对象
 * @author: Xiong weiqiang
 * @create: 2025-04-13 15:09
 */
@Data
public class RuleDataSaveRto {
    private Long id;
    @NotBlank(message = "规则组不能为空")
    private String ruleName;
    @NotNull(message = "规则生效开始日期不能为空")
    private Date validStartTime;
    @NotNull(message = "规则生效结束日期不能为空")
    private Date validEndTime;
    @NotNull(message = "优先级不能为空")
    private Integer priority;

    @NotEmpty(message = "规则组不能为空")
    private List<RuleConditionGroupSaveRto> groupSaveDtos;
    public RuleDataSaveDto toRuleDataSaveDto(){
        RuleDataSaveDto ruleDataSaveDto = new RuleDataSaveDto();
        ruleDataSaveDto.setId(this.id);
        ruleDataSaveDto.setPriority(this.priority);
        ruleDataSaveDto.setRuleName(this.ruleName);
        ruleDataSaveDto.setValidStartTime(this.validStartTime);
        ruleDataSaveDto.setValidEndTime(this.validEndTime);
        List<RuleConditionGroupSaveDto> saveDtoList = this.groupSaveDtos.stream().map(ruleConditionGroupSaveRto -> {
            RuleConditionGroupSaveDto ruleConditionGroupSaveDto = new RuleConditionGroupSaveDto();
            BeanUtil.copyProperties(ruleConditionGroupSaveRto, ruleConditionGroupSaveDto,"conditionSaveDtoList");
            if (ruleConditionGroupSaveRto.getConditionSaveDtoList() == null){
                ruleConditionGroupSaveDto.setConditioSaveDtoList(List.of());
            }else{
                List<RuleConditionSaveDto> list = BeanUtil.copyToList(ruleConditionGroupSaveRto.getConditionSaveDtoList(), RuleConditionSaveDto.class);
                ruleConditionGroupSaveDto.setConditioSaveDtoList(list);
            }
            return ruleConditionGroupSaveDto;
        }).toList();
        ruleDataSaveDto.setGroupSaveDtoList(saveDtoList);
        return ruleDataSaveDto;
    }
    @Data
    public static class RuleConditionGroupSaveRto{
        @NotNull(message = "执行类型不能为空")
        private String actionType;
        @NotNull(message = "执行值不能为空")
        private String actionValue;
        private List<RuleConditionSaveRto> conditionSaveDtoList;
    }
    @Data
    public static class RuleConditionSaveRto{
        @NotNull(message = "参数类型不能为空")
        private String conditionParamKey;

        @NotNull(message = "操作符不能为空")
        private String matchTypeKey;

        @NotNull(message = "值不能为空")
        private String matchValue;
    }
}